package com.specter.utils;

/**
 * Note:字符串的相关操作，已经继承 common-lang3
 * 
 * @author Liang.Wang
 * @version Mar 9, 2017
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

	/**
	 * 字符串驼峰转下划线格式
	 * 
	 * @param param 需要转换的字符串
	 * @return 转换好的 <b style="color:red;">大写</b> 字符串
	 */
	public static String toDataName(String javaName) {
		if (isEmpty(javaName)) {
			return EMPTY;
		}
		int len = javaName.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = javaName.charAt(i);
			if (Character.isUpperCase(c) && i > 0) {
				sb.append('_');
			}
			sb.append(Character.toUpperCase(c));
		}
		return sb.toString();
	}

	/**
	 * 字符串下划线转驼峰格式
	 * 
	 * @param param 需要转换的字符串
	 * @return 转换好的字符串
	 */
	public static String toJavaName(String dataName) {
		if (isEmpty(dataName)) {
			return EMPTY;
		}
		String temp = dataName.toLowerCase();
		int len = temp.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = temp.charAt(i);
			if (c == '_' && ++i < len) { // 跳过一个字母
				sb.append(Character.toUpperCase(temp.charAt(i)));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 字符串驼峰转中间连接线小写格式
	 * 
	 * @param param 需要转换的字符串
	 * @return 转换好的字符串
	 */
	public static String toViewName(String javaName) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < javaName.length(); i++) {
			if (i != 0 && Character.isLowerCase(javaName.charAt(i - 1)) && Character.isUpperCase(javaName.charAt(i))) {
				sb.append("-");
			}
			sb.append(Character.toLowerCase(javaName.charAt(i)));
		}
		return sb.toString();
	}

	/**
	 * 字符串驼峰转下划线小写格式
	 * 
	 * @param param 需要转换的java字符串
	 * @return 转换好的字符串
	 */
	public static String toSqlName(String javaName) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < javaName.length(); i++) {
			if (i != 0 && Character.isLowerCase(javaName.charAt(i - 1)) && Character.isUpperCase(javaName.charAt(i))) {
				sb.append("_");
			}
			sb.append(Character.toLowerCase(javaName.charAt(i)));
		}
		return sb.toString();
	}

	/**
	 * 首字母大写
	 * 
	 * @param name 名字
	 * @return
	 */
	public static String cap_first(String name) {
		return Character.toUpperCase(name.charAt(0)) + name.substring(1); // 首字母大写
	}

	/**
	 * 对 +IDS_%s+TBL 字符串进行前缀和后缀的操作
	 * 
	 * @param fixPattern 前缀后缀的规则
	 * @param str        目标的字符串
	 * 
	 * @return 转换好的字符串
	 */
	public static String fix(String fixPattern, String str) {
		String prefix = fixPattern.substring(0, fixPattern.indexOf("%s"));
		prefix = (!prefix.startsWith("+") && !prefix.startsWith("-")) ? "+" + prefix : prefix;

		String suffix = fixPattern.substring(fixPattern.indexOf("%s") + 2);
		suffix = (!suffix.startsWith("+") && !suffix.startsWith("-")) ? "+" + suffix : suffix;

		if (prefix.startsWith("-") && str.indexOf(prefix.substring(1)) > -1) {
			str = str.substring(prefix.length() - 1);
		} else if (prefix.startsWith("+")) {
			str = prefix.substring(1) + str;
		}

		if (suffix.startsWith("-") && str.indexOf(suffix.substring(1), 1) > -1) {// 至少要多一个字母
			str = str.substring(0, str.indexOf(suffix.substring(1)));
		} else if (suffix.startsWith("+")) {
			str = str + suffix.substring(1);
		}
		return str;

	}

	/**
	 * 将中文转换成 &#x1111;的unicode编码
	 * 
	 * @param input 中文字符串
	 * @return unicode编码
	 */
	public static String escapeHex(String input) {
		StringBuilder escaped = new StringBuilder(input.length() * 2);
		for (int i = 0; i < input.length(); i++) {
			char character = input.charAt(i);
			if (character > 10000) {
				escaped.append("&#x");
				escaped.append(Integer.toString(character, 16));
				escaped.append(";");
			} else {
				escaped.append(character);
			}
		}
		return escaped.toString();
	}
}
